import React, { useEffect, useState } from 'react';
import { Modal, Form, Space } from 'antd';
import FormCondition from '@/components/FormCondition';
import { connect, history } from 'umi';
import { Button, Row, Col } from 'antd';
import aliOssUpload from '@/utils/aliOssUpload';
const UpdateAnswerUser = (props) => {
    const { visible = false, onClose, loading, dispatch, handleCallback, } = props;
    const { show = false, detail = {} } = visible;
    const { id } = detail;
    const [form] = Form.useForm();
    // 确认提交
    const handleUpAudit = () => {
        form.validateFields().then(async (values) => {
            const { profile, subjectIds, tagIds, recommendedHomeTeacher, recommendedHomeRecruitment } = values
            const img = await aliOssUpload(profile)
            dispatch({
                type: 'answering/fakeSaveTeacher',
                payload: {
                    ...values,
                    profile: img.toString(),
                    subjectIds: subjectIds.toString(),
                    tagIds: tagIds.toString(),
                    recommendedHomeTeacher: recommendedHomeTeacher ? '1' : '0',
                    recommendedHomeRecruitment: recommendedHomeRecruitment ? '1' : '0',
                    id
                },
                callback: () => {
                    onClose()
                    handleCallback()
                }
            })
        });
    };
    const fetchSearchList = () => {
        dispatch({
            type: 'answering/fetchListSubject',
            callback: (e) => {
                setSubjectList(e)
            }
        })
        dispatch({
            type: 'answering/fetchListTeacherTag',
            callback: (e) => {
                setTagList(e)
            }
        })
    }
    useEffect(() => {
        fetchSearchList()
    }, [])
    const [tagList, setTagList] = useState([])
    const [subjectList, setSubjectList] = useState([])
    const formItems = [
        { label: '老师姓名', name: 'teacherName', },
        { label: '头像', name: 'profile', type: 'upload', maxFile: 1 },
        {
            label: '学科', name: 'subjectIds',
            type: 'multiple',
            fieldNames: {
                label: 'subjectName',
                value: 'id'
            },
            rules: [{ required: false }],
            select: subjectList
        },
        {
            label: '标签', name: 'tagIds', type: 'multiple',
            fieldNames: {
                label: 'tagName',
                value: 'id'
            },
            select: tagList
        },
        { label: '描述', name: 'description', type: 'textArea' },
        {
            label: '绑定员工',
            name: 'adminName',
            type: 'user',
            onChange: (e, { option }) => {
                form.setFieldsValue({
                    adminId: option.id
                })
            },
            fieldNames: {
                label: 'username',
                value: 'username'
            },
        },
        {
            label: '绑定员工',
            name: 'adminId',
            hidden: true,
            type: 'user',
            fieldNames: {
                label: 'username',
                value: 'id'
            },
        },
    ];

    const modalProps = {
        title: '编辑',
        width: 800,
        visible: show,
        maskClosable: true,
        destroyOnClose: true,
        zIndex: 1001,
        confirmLoading: loading,
    };

    return (
        <Modal {...modalProps} onCancel={onClose} onOk={handleUpAudit}>
            <FormCondition formItemLayouts={{
                labelCol: {

                },
                wrapperCol: {

                },
            }} form={form} initialValues={detail} formItems={formItems}></FormCondition>
        </Modal >
    );
};

export default connect(({ order, loading }) => ({
    loading: loading.effects['answering/fakeSaveTeacher'],
}))(UpdateAnswerUser);
;
